#include "linfolistwidget.h"
#include <QHeaderView>
#include <QTime>
#include "contextmenu.h"
#include <QMenu>
#include <QAction>
#include <QFileDialog>
#include <QFile>
#include <QApplication>
#include <QClipboard>
#include <QMimeData>

#include <QDebug>

LInfoListWidget::LInfoListWidget(QWidget *parent) :
    QListWidget(parent),
    m_contextMenu(new ContextMenu(this)),
    m_copyAct(new QAction(QString("Copy"), this)),
    m_saveAct(new QAction(QString("Save"), this)),
    m_clearAct(new QAction(QString("Clear"), this)),
    autoScroll(true)
{
    this->setObjectName(QString::fromUtf8("InfoListWidget"));
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);
    this->setProperty("showDropIndicator", QVariant(false));
    this->setAlternatingRowColors(true);

    m_contextMenu->clear();
    m_contextMenu->clearSubMenuActsFilter();

    m_contextMenu->addAction(m_copyAct);
    m_contextMenu->addAction(m_saveAct);
    m_contextMenu->addSeparator();
    m_contextMenu->addAction(m_clearAct);

    connect(m_copyAct, &QAction::triggered, this, &LInfoListWidget::copyInfo);
    connect(m_saveAct, &QAction::triggered, this, &LInfoListWidget::saveInfo);
    connect(m_clearAct, &QAction::triggered, this, &LInfoListWidget::clearInfo);
}

LInfoListWidget::~LInfoListWidget()
{
    m_contextMenu->clear();
    delete m_saveAct;
    delete m_clearAct;
    delete m_contextMenu;
}

void LInfoListWidget::setAutoScrollToBottom(bool enable)
{
    autoScroll = enable;
}

void LInfoListWidget::showInfo(const QString &info, const QByteArray &detail)
{
    QListWidgetItem* topit = new QListWidgetItem(0);
    if (!topit) return;

    QString ts(QTime::currentTime().toString("HH:mm:ss zzz"));
    QString text;

    text = QString("[") + ts + QString("]    ") + info;

    topit->setText(text);

    frames.append(detail);

    this->addItem(topit);

    packInfo();
}

QByteArray LInfoListWidget::frame(int row)
{
    if (frames.count() > 0) {
        return frames.at(row);
    } else {
        return QByteArray();
    }
}

void LInfoListWidget::packInfo()
{
    if(this->count() > INFO_MAX_ITEM){
        this->model()->removeRows(0, INFO_MAX_RM);
        for (int i = 0; i < INFO_MAX_RM; i++) {
            frames.removeFirst();
        }
    }
    if (autoScroll) {
        this->scrollToBottom();
    }
}

void LInfoListWidget::contextMenuEvent(QContextMenuEvent *e)
{
    if(this->count() == 0){
        QListWidget::contextMenuEvent(e);
        return;
    }
    m_contextMenu->exec(QCursor::pos());
}

void LInfoListWidget::copyInfo()
{
    QClipboard *clipboard = QApplication::clipboard();
    auto *mimeData = new QMimeData;
    mimeData->setText(this->currentItem()->text());

    clipboard->setMimeData(mimeData);
}

void LInfoListWidget::saveInfo()
{
    QString file;

    file = QFileDialog::getSaveFileName(this, "Save Info",
                                        "./",
                                        "*.txt");
    if (file.isEmpty()) return;

    QFile fi(file);
    fi.open(QFile::WriteOnly);

    for (int i = 0; i < this->count(); i++) {
        QListWidgetItem* topit = this->item(i);
        fi.write(topit->text().toUtf8());
        fi.write("\n");
    }

    fi.close();
}

void LInfoListWidget::clearInfo()
{
    this->model()->removeRows(0, this->count());
    frames.clear();
}
